﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace EEDS.Core
{
    public class Palette
    {
        private Color[] colours;
        private string name;
        private Game game;
        private int indexOfBackColour;

        public Palette(Game game, string name, Color[] colours, int indexOfBackColour)
        {
            this.game = game;
            this.name = name;
            this.indexOfBackColour = indexOfBackColour;

            this.colours = colours;
        }

        public Palette(Game game, string name, int[] colours, int indexOfBackColour) : this(game, name, Util.ConvertIntArrayToColours(colours), indexOfBackColour) { }
        public Palette(Game game, string name, Color[] colours) : this(game, name, colours, colours.GetUpperBound(0)) { }
        public Palette(Game game, string name, int[] colours) : this(game, name, colours, colours.GetUpperBound(0)) { }
        public Palette(string name, Color[] colours) : this(Game.None, name, colours) { }
        public Palette(string name, int[] colours) : this(Game.None, name, colours) { }
        public Palette(int[] colours) : this("", colours) { }
        public Palette(Color[] colours) : this("", colours) { }

        public static bool ConfirmMatch(Bitmap emblem, Palette palette)
        {
            for (int x = 0; x < emblem.Width; x++)
            {
                for (int y = 0; y < emblem.Height; y++)
                {
                    Color pixel = emblem.GetPixel(x, y);
                    bool flag = false;

                    for (int k = 0; k < palette.Colours.Length; k++)
                    {
                        if (palette.Colours[k] == pixel)
                        {
                            flag = true;
                            break;
                        }
                    }

                    if (!flag)
                        return false;
                }
            }

            return true;
        }

        public bool ConfirmMatch(Bitmap emblem)
        {
            return Palette.ConfirmMatch(emblem, this);
        }

        public int GetIndexOf(Color colour)
        {
            for (int i = 0; i < Colours.Length; i++)
            {
                if (colour == Colours[i])
                    return i;
            }

            return -1;
        }

        public static Palette[] FindMatchingPalettes(Bitmap emblem, Palette[] palettes)
        {
            List<Palette> soFar = new List<Palette>();

            foreach (Palette palette in palettes)
            {
                bool flag = false;

                for (int x = 0; x < emblem.Width; x++)
                {
                    for (int y = 0; y < emblem.Height; y++)
                    {
                        Color colour = emblem.GetPixel(x, y);
                        flag = false;

                        foreach (Color clr in palette.Colours)
                        {
                            if (clr == colour)
                            {
                                flag = true;
                                break;
                            }
                        }

                        if (!flag)
                            break;
                    }

                    if (!flag)
                        break;
                }

                if (flag)
                    soFar.Add(palette);
            }

            return soFar.ToArray();
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        public Game Game
        {
            get
            {
                return game;
            }
        }

        public Color[] Colours
        {
            get
            {
                return colours;
            }
        }

        public int IndexOfBackColour
        {
            get
            {
                return indexOfBackColour;
            }
        }

        public Color BackColour
        {
            get
            {
                return colours[IndexOfBackColour];
            }
        }
    }
}
